package vg.modules.notepad.components.textComponent;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;

/**
 * This class realizes search line for notepad.
 * @author tzolotuhin
 */
public class TextSearchLine {
	// Components
	private final JPanel view;
	private final JPanel northPanel;
	private final JPanel centerPanel;
	private final JPanel southPanel;
	// Feed back
	private final JTextComponent fbTextComponent;
	// Defines
	private static final Color DEF_NORTH_PANEL_COLOR = new Color(244, 200, 45);
	private static final Color DEF_NORTH_PANEL_BORDER_COLOR = new Color(160, 160, 160);
	private static final Color DEF_NODE_COLOR = new Color(252, 241, 202);
	private static final Color DEF_NODE_BORDER_COLOR = new Color(246, 211, 87);	
	private static final Dimension DEF_SIZE_PANEL = new Dimension(12,6);
	/**
	 * Constructor.
	 */
	public TextSearchLine(JTextComponent fbComponent) {
		// create components
		this.fbTextComponent = fbComponent;
		this.view = new JPanel(new GridBagLayout());
		this.northPanel = new JPanel(null);
		this.centerPanel = new JPanel(new GridBagLayout());
		this.southPanel = new JPanel(null);
		// create panels
		GridBagConstraints gbc = null;
		this.northPanel.setPreferredSize(new Dimension(10,10));
		this.northPanel.setMinimumSize(new Dimension(10,10));
		this.northPanel.setBackground(DEF_NORTH_PANEL_COLOR);
		this.northPanel.setBorder(BorderFactory.createLineBorder(DEF_NORTH_PANEL_BORDER_COLOR));
		this.northPanel.setToolTipText("Results: 0");
		gbc = new GridBagConstraints(0,0, 1,1, 0,0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2,2,2,2), 0,0);
		this.view.add(this.northPanel, gbc);
		
		gbc = new GridBagConstraints(0,1, 1,1, 1,1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0,0);
		this.view.add(centerPanel, gbc);
		
		this.southPanel.setPreferredSize(new Dimension(10,10));
		this.southPanel.setMinimumSize(new Dimension(10,10));
		gbc = new GridBagConstraints(0,2, 1,1, 0,0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2,2,2,2), 0,0);
		this.view.add(this.southPanel, gbc);
	}
	
	public synchronized JPanel getView() {
		return(this.view);
	}
	/**
	 * This method sets search result to search line.
	 * @param result - list of results.
	 * @param countLine - count of line.
	 */
	public synchronized void setSearchResult(final Map<Integer,Integer>result, final int countLine) {
		GridBagConstraints gbc = null;
		this.centerPanel.removeAll();
		int i = 0;
		Object[] array = result.keySet().toArray();
		Arrays.sort(array);
		for(Object buf : array) {
			float alpha = ((Integer)buf / (float)countLine);
			
			if(alpha < 0) alpha = 0;
			if(alpha > 1) alpha = 1;
			
			JPanel node = new JPanel(null);
			node.setPreferredSize(DEF_SIZE_PANEL);
			node.setMinimumSize(DEF_SIZE_PANEL);
			node.setBackground(DEF_NODE_COLOR);
			node.setBorder(BorderFactory.createLineBorder(DEF_NODE_BORDER_COLOR));
			node.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			
			int startPos = (Integer)buf;
			int finalPos = result.get(buf);
			node.addMouseListener(new NodeMouseAdapter(startPos, finalPos, fbTextComponent));
			
			gbc = new GridBagConstraints(0,i, 1,1, 0,alpha, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0,1,0,1), 0,0);
			this.centerPanel.add(node, gbc);
			i++;
		}
		this.northPanel.setToolTipText("Results: " + array.length);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TextSearchLine.this.centerPanel.updateUI();
			}
		});
	}
	/**
	 * This method resets search line.
	 */
	public synchronized void reset() {
		this.centerPanel.removeAll();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TextSearchLine.this.centerPanel.updateUI();
			}
		});
	}
	///////////////////////////////////////////////////////////////////////////
	// PRIVATE CLASSES
	///////////////////////////////////////////////////////////////////////////
	private static class NodeMouseAdapter extends MouseAdapter {
		private static final long serialVersionUID = 1L;
		private final int startPos;
		private final int finalPos;
		private final JTextComponent fbTexttComponent;
		/**
		 * Constructor.
		 */
		public NodeMouseAdapter(final int startPos, final int finalPos, final JTextComponent fbTextComponent) {
			this.startPos = startPos;
			this.finalPos = finalPos;
			this.fbTexttComponent = fbTextComponent;
		}
		/**
		 * Action
		 */
		public void mouseClicked(MouseEvent e) {
			this.fbTexttComponent.requestFocus();
			this.fbTexttComponent.select(this.startPos, this.finalPos);
			super.mouseClicked(e);
		}
	}
}
